#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <ostream>
#include <algorithm>
#include <map>
#include <queue>


using namespace std;

struct data
	{


		string word;
		int n;

		data(string word, int n)
		{
			this->word = word;
			this->n = n;
		}


		friend bool operator< (const data &lhs, const data& rhs)
		{
			return lhs.n<rhs.n;
		}

		

	};

class Ordrakning
{
private:
	typedef pair<string, int> value;
	map <string, int> wordMap;
	priority_queue<data> pq;

public:



	Ordrakning(void)
	{
	}

	void ReadFromFile()
	{
		string ord;

		ifstream fin;
		fin.open("nils_holgersson.txt");
		while (fin.good())
		{
			fin >> ord;
			wordMap[ord]++;
		}
	}

	void frequentWord()
	{
		int Maxvalue = 0;
		string Word;
		for (auto it = wordMap.begin(); it != wordMap.end(); it++)
		{
			if (it->second > Maxvalue)
			{
				Maxvalue = it->second;
				Word = it->first;
				
			}
		}
		

		cout << "Most used word is: " << Word << " And amount of times used: " << Maxvalue << endl << endl;;
		for (auto it = wordMap.begin(); it != wordMap.end(); it++)
		{
			pq.push(data(it->first, it->second));
			
		}

		int k = 0;

		cout << "20 most used words are: " << endl << endl;

		while (!pq.empty() && k < 20)
		{
			cout << pq.top().n << " " <<  pq.top().word << endl;
			pq.pop();
			k++;
			
		}
		 
		

		

		
	}

	
		
	

	~Ordrakning(void)
	{
	}
};

